(function() {
    var defaults = {
        // renderTo: $(document.body),
        autoInit: false,
        autoDestory: false,
        icon: true,
        msg: ''
    }, callback = Tgm.callback;
    var Mask = function(opts) {
        this.id = Tgm.id();
        $.extend(this, defaults, opts);
		this.renderTo = opts.renderTo || $(document.body);
        var obj = $.extend({}, this);
        this.get = function(key) {
            return obj[key];
        };
        this.set = function(key, value) {
            return obj[key] = value;
        };
        if (this.autoInit) {
            this.init();
        }
    };
    Mask.prototype = (function() {
        var helper = {
            init: {
                action: function() {
                    var t = this;
                    helper.init.html.call(t);
                    helper.init.node.call(t);
                    helper.init.style.call(t);
                    helper.init.event.call(t);
                },
                html: function() {
                    var t = this, r = t.get('renderTo');
                    var html = '<div id="' + t.get('id') + '" class="mmc-el-masked"></div>';
                    html += '<div class="mmc-el-mask-msg ' + (t.get('icon') ? 'mmc-mask-loading' : '') + '"><div>' + t.get('msg') + '</div></div>';
                    t.get('renderTo').append(html);
                },
                node: function() {
                    var t = this;
                    t.set('maskNode', $('#' + t.get('id')));
                    t.set('msgNode', t.get('maskNode').next());
                    t.set('textNode', t.get('msgNode').find('div:first-child'));
                },
                style: function() {
                    var t = this;
                    t.get('maskNode').hide();
                    t.get('msgNode').hide();
                },
                event: function() {
                
                }
            },
            render: {
                action: function() {
                    var t = this, win, renderTo = t.get('renderTo'), p = renderTo.css('position'), maskNode = t.get('maskNode'), msgNode = t.get('msgNode');
                    if (Tgm.ie6) {
                        helper.ie6.select.hide.apply(t);
                    }
                    var top, left;
                    if (renderTo[0] === document.body) {
                        win = $(window);
                        top = (win.height() - msgNode.height()) / 2 + win.scrollTop();
                        left = (win.width() - msgNode.width()) / 2 + win.scrollLeft();
                        maskNode.css({
                            display: 'block',
                            top: win.scrollTop(),
                            left: win.scrollLeft(),
                            width: win.width(),
                            height: win.height()
                        });
                        t.set('maskFixed', new Tgm.Fixed({
                            renderTo: maskNode
                        }));
                        t.get('maskFixed').render();
                    } else {
                        renderTo.addClass('mmc-masked');
                        if (!p || p == 'static') {
                            renderTo.addClass('mmc-masked-relative');
                        }
                        top = (maskNode.height() - msgNode.height()) / 2;
                        left = (maskNode.width() - msgNode.width()) / 2;
                        maskNode.show();
                    }
                    if (t.get('msg') !== false) {
                        msgNode.css({
                            display: 'block',
                            'z-index': Tgm.get('zIndex'),
                            top: top,
                            left: left
                        });
                        if (renderTo[0] === document.body) {
                            t.set('msgFixed', new Tgm.Fixed({
                                renderTo: msgNode
                            }));
                            t.get('msgFixed').render();
                        }
                    }
                    helper.render.event.call(this);
                },
                event: function() {
                    helper.event.bind.resize.call(this);
                }
            },
            unrender: {
                action: function() {
                    var t = this, renderTo = t.get('renderTo'), maskNode = t.get('maskNode'), msgNode = t.get('msgNode');
                    renderTo.removeClass('mmc-masked mmc-masked-relative');
                    if (Tgm.ie6) {
                        helper.ie6.select.show.apply(t);
                    }
                    maskNode.hide();
                    msgNode.hide();
                    if (renderTo[0] === document.body) {
                        t.get('maskFixed').unrender();
                        if (t.get('msg') !== false) {
                            t.get('msgFixed').unrender();
                        }
                    }
                    helper.unrender.event.call(this);
                },
                event: function() {
                    helper.event.unbind.resize.call(this);
                }
            },
            destory: {
                action: function() {
                    this.get('maskNode').remove();
                    this.get('msgNode').remove();
                }
            },
            ie6: {
                select: {
                    hide: function() {
                        var t = this;
                        t.get('renderTo').find('select').filter(function() {
                            return this.style.visibility !== 'hidden';
                        }).addClass('select-hide');
                    },
                    show: function() {
                        var t = this;
                        t.get('renderTo').find('select.select-hide').removeClass('select-hide');
                    }
                }
            },
            rule: {},
            event: (function() {
                var resizeFns = {};
                return {
                    bind: {
                        resize: function() {
                            var t = this;
                            resizeFns[t.get('id')] = function() {
                                t.unrender();
                                t.render();
                            };
                            $(window).bind('resize', resizeFns[t.get('id')]);
                        }
                    },
                    unbind: {
                        resize: function() {
                            var t = this;
                            $(window).unbind('resize', resizeFns[t.get('id')]);
                        }
                    }
                };
            })()
        };
        return {
            constructor: Mask,
            init: function() {
                if (this.get('isInit')) {
                    return this;
                } else {
                    if (callback(this.get('bi'), this.get('bip'), this.get('bis') || this) !== false) {
                        helper.init.action.apply(this);
                        this.set('isInit', true);
                        callback(this.get('ai'), this.get('aip'), this.get('ais') || this);
                        return this;
                    }
                    return false;
                }
            },
            render: function() {
                if (!this.get('isInit')) {
                    this.init();
                }
                if (this.get('isRender')) {
                    return this;
                } else {
                    if (callback(this.get('br'), this.get('brp'), this.get('brs') || this) !== false) {
                        helper.render.action.apply(this);
                        this.set('isRender', true);
                        callback(this.get('ar'), this.get('arp'), this.get('ars') || this);
                    }
                }
            },
            unrender: function() {
                if (this.get('isRender')) {
                    if (callback(this.get('bc'), this.get('bcp'), this.get('bcs') || this) !== false) {
                        helper.unrender.action.apply(this);
                        this.set('isRender', false);
                        callback(this.get('ac'), this.get('acp'), this.get('acs') || this);
                        if (this.get('autoDestory')) {
                            this.destory();
                        }
                        return this;
                    }
                } else {
                    return false;
                }
            },
            destory: function() {
                if (this.get('isInit')) {
                    if (callback(this.get('bd'), this.get('bdp'), this.get('bds') || this) !== false) {
                        helper.destory.action.apply(this);
                        this.set('isInit', false);
                        callback(this.get('ad'), this.get('adp'), this.get('ads') || this);
                        return this;
                    }
                } else {
                    return false;
                }
            }
        };
    })();
	Tgm.reg('Mask', Mask);
})();
